﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Bussiness;

namespace EWebsite.Admin.Products {
    public partial class Add : System.Web.UI.Page {
        private DataTable _tblCategories;
        public DataTable TblCategories { get { return this._tblCategories; } }
        
        protected void Page_Load(object sender, EventArgs e) {
            if(!IsPostBack)
                _DataBind();
        }

        protected void _DataBind() {
            CategoryBuss buss = new CategoryBuss();
            _tblCategories = buss.getCategories();

            drpCategory.DataSource = _tblCategories;
            drpCategory.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e) {            
            try {
                String photoUrl = "";
                int catID = int.Parse(drpCategory.SelectedValue);
                //int catID = int.Parse(Request["categoryTree"]);
                String name = txtName.Text.Trim();
                String description = txtDescription.Text.Trim();
                String otherInfo = txtOtherInfo.Text.Trim();
                float price = float.Parse(txtPrice.Text);                
                float width = float.Parse(txtWidth.Text);
                float heigth = float.Parse(txtHeight.Text);
                float length = float.Parse(txtLength.Text);
                float weigth = float.Parse(txtWeight.Text);
                int Instock = int.Parse(txtInstock.Text);
                int quantityAtLeast = int.Parse(txtQuantityAtLeast.Text);

                if (fuImage.HasFile) {
                    if (Helper.isImageAllow(fuImage.FileName)) { //if is image
                        String fileName = Helper.convertToSlug(fuImage.FileName);
                        //upload
                        fuImage.SaveAs(Server.MapPath("~") + "images/uploads/" + fileName);
                        photoUrl = ("/images/uploads/" + fileName).Trim();
                    }
                }

                ProductBuss buss = new ProductBuss();
                int id = buss.add(name, catID, price, photoUrl, description, otherInfo, Instock, width, heigth, length, weigth, quantityAtLeast);
                Response.Redirect("Edit.aspx?p=" + id);
            }
            catch (Exception ex) {
                return;
            }
        }
                
    }
}